package com.lakeworks.service.impl;

import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.common.enums.OprProductType;
import com.lakeworks.common.enums.PickupType;
import com.lakeworks.common.enums.ShipMentType;
import com.lakeworks.common.exception.response.ResponseException;
import com.lakeworks.common.utils.StringUtils;
import com.lakeworks.domain.OprPickup;
import com.lakeworks.domain.OprProduct;
import com.lakeworks.domain.OprShipment;
import com.lakeworks.domain.status.*;
import com.lakeworks.domain.vo.*;
import com.lakeworks.mapper.*;
import com.lakeworks.service.IOprIndexService;
import com.lakeworks.service.IOprOrderService;
import com.lakeworks.service.api.MerchantAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.Optional.ofNullable;

@Service
public class OprOrderServiceImpl implements IOprOrderService {


    @Autowired
    private MerchantAsync merchantAsync;


    @Resource
    private OprPickupMapper oprPickupMapper;


    @Resource
    private OprShipmentMapper oprShipmentMapper;


    @Override
    public String getOrderDetails(Long channelId, String orderNo) {
        return merchantAsync.getOrderDetails(orderNo, channelId);
    }

    @Override
    public AjaxResult getOrderExpress(String expressNo) {
        Long channelId = ofNullable(oprPickupMapper.selectPickupByExpressNo(expressNo))
                .map(PickupVo::getChannelId)
                .orElse(ofNullable(oprShipmentMapper.queryByExpressNo(expressNo))
                        .map(OprShipment::getChannelId)
                        .orElse(null));
        if(channelId == null){
            return AjaxResult.error("物流单号错误");
        }
        return AjaxResult.success(merchantAsync.getOrderExpress(expressNo,channelId));
    }


    @Override
    public AjaxResult getOrderExpressNos(String orderNo) {
        Map<String, List<String>> map = new HashMap<>();
        List<OprPickup> pickups = oprPickupMapper.selectOprPickupList(OprPickup.builder().orderNo(orderNo).build());
        List<OprShipment> oprShipments = oprShipmentMapper.selectShipmentList(OprShipment.builder().orderNo(orderNo).build());
        map.put("pickupExpressNos", pickups.stream().map(OprPickup::getExpressNo).filter(StringUtils::isNotBlank).toList());
        map.put("shipmentExpressNos", oprShipments.stream().map(OprShipment::getExpressNo).filter(StringUtils::isNotBlank).toList());
        return AjaxResult.success(map);
    }



}
